<!--车辆管理-->
<template>
	<el-dialog v-model="visible" :title="(!dataForm.id ? '新增' : '修改') + '车辆'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-row>
				<el-col :span="12">
					<el-form-item label="车辆名称" prop="name"> <el-input v-model="dataForm.name" placeholder="车辆名称"></el-input> </el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="车牌号" prop="plateNumber"> <el-input v-model="dataForm.plateNumber" placeholder="车牌号"></el-input> </el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="车型" prop="carTypeId">
						<el-tree-select
							v-model="dataForm.carTypeId"
							:data="dictList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="分类" prop="classifyId">
						<el-input v-model="dataForm.classifyId" placeholder="分类"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="品牌型号" prop="modelStr">
						<el-input v-model="dataForm.modelStr" placeholder="品牌型号"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="车座" prop="saddleNumberStr">
						<el-input v-model="dataForm.saddleNumberStr" placeholder="车座" type="number" max="100" min="1">
							<template #append>座</template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="裸车价" prop="carPrice">
						<el-input v-model="dataForm.carPrice" placeholder="裸车价">
							<template #append>元</template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="交强险到期日期" prop="ctiEndDate">
						<ElDatePicker v-model="dataForm.ctiEndDate" placeholder="交强险到期日期" />
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="商业险到期日期" prop="ciEndDate">
						<ElDatePicker v-model="dataForm.ciEndDate" placeholder="商业险到期日期" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="年检日期" prop="asDate">
						<ElDatePicker v-model="dataForm.asDate" placeholder="年检日期" />
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="上传照片" prop="imgUrl">
				<UploadImgView v-model="dataForm.imgUrl"></UploadImgView>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="可用范围" prop="isAll">
				<el-input v-model="dataForm.isAll" placeholder="可用范围"></el-input>
			</el-form-item>
			<el-form-item label="允许用车申请" prop="isEnable">
				<el-switch
					v-model="dataForm.isEnable"
					:active-value="'1'"
					:inactive-value="'0'"
					active-text="启用"
					inline-prompt
					inactive-text="停用"
					placeholder=""
				/>
			</el-form-item>
			<el-form-item label="用车需审批" prop="isUsedCars">
				<el-switch
					v-model="dataForm.isUsedCars"
					:active-value="'1'"
					:inactive-value="'0'"
					active-text="启用"
					inline-prompt
					inactive-text="停用"
					placeholder=""
				/>
			</el-form-item>
			<el-form-item label="还车需审批" prop="isReturnCar">
				<el-switch
					v-model="dataForm.isReturnCar"
					:active-value="'1'"
					:inactive-value="'0'"
					active-text="启用"
					inline-prompt
					inactive-text="停用"
					placeholder=""
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useCarApi, useCarSubmitApi } from '@/api/nb/car/car'
import UploadImgView from '@/views/nb_module/image/upload-img-view.vue'
import { findListByDictId } from '@/api/nb/business/dictionaries_subset'

const emit = defineEmits(['refreshDataList'])
const props = defineProps(['code'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',

	name: '',
	plateNumber: '',
	carTypeId: '',
	classifyId: '',
	modelStr: '',
	saddleNumberStr: '',
	carPrice: '',
	ctiEndDate: '',
	ciEndDate: '',
	asDate: '',
	imgUrl: '',
	remark: '',
	isAll: '',
	isEnable: '1',
	isUsedCars: '1',
	isReturnCar: '',
	isStatr: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getCar(id)
	}
}

const getCar = (id: number) => {
	useCarApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useCarSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

const dictList=ref()
const getDictList = () => {
	findListByDictId({ params: { code: props.code } }).then(res => {
		dictList.value = res.data
	})
}
getDictList()

defineExpose({
	init
})
</script>
